<?php

class CBAuthManager extends CDbAuthManager {
    public function __construct() {
        //$this->createRoles();
    }

    private function createRoles(){
        $this->init();

        $this->clearAll();

        $this->createRole('user', 'Normal Authenticated User', 'return (!Yii::app()->user->isGuest && (Yii::app()->user->status==User::STATUS_NORMAL));');
        $this->createRole('unchecked_user', 'Unchecked User', 'return (Yii::app()->user->status==User::STATUS_UNCHECKED);');
        $this->createRole('blocked_user', 'Blocked User', 'return (Yii::app()->user->status==User::STATUS_BLOCKED);');
        $this->createRole('deleted_user', 'Deleted User', 'return (Yii::app()->user->status==User::STATUS_DELETED);');
        $this->createRole('guest', 'Guest', 'return Yii::app()->user->isGuest;');
        $this->createRole('author','Edit Own Post', 'return Yii::app()->user->id==$params["post"]->authorId;');

        $this->createRole('admin', 'Administrator');
        $this->createRole('super_admin', 'Super-Administrator');

        $this->createRole('moder', 'Moderator');
        $this->createRole('super_moder', 'Moderator');

        //задачи
        $this->createTask('updateProfile', 'Update Own Profile', 'return Yii::app()->user->id==$params["post"]->userId;');
        $this->createTask('manage_users');
        $this->createTask('manage_news');
        $this->createTask('manage_lib');
        $this->createTask('manage_posts');
        $this->createTask('manage_moders');
        $this->createTask('manage_admins');

        // операции над библиотекой
        $this->createOperation('create_record', 'Create Library Record');
        $this->createOperation('update_record', 'Update Library Record');
        $this->createOperation('delete_record', 'Delete Library Record');
        $this->createOperation('select_record', 'Select Library Record');

        // операции для комментариев и советов
        $this->createOperation('create_post', 'Create Post');
        $this->createOperation('update_post', 'Update Post');
        $this->createOperation('delete_post', 'Delete Post');
        $this->createOperation('select_post', 'Select Post');

        $this->addItemChild('guest', 'select_record');
        $this->addItemChild('guest', 'select_post');

        $this->addItemChild('user', 'guest');

        $this->addItemChild('author', 'update_record');
        $this->addItemChild('author', 'update_post');

        $this->addItemChild('user', 'author');
        $this->addItemChild('user', 'updateProfile');
        $this->addItemChild('user', 'create_record');

        $this->addItemChild('manage_lib', 'create_record');
        $this->addItemChild('manage_lib', 'update_record');
        $this->addItemChild('manage_lib', 'delete_record');
        $this->addItemChild('manage_lib', 'select_record');

        $this->addItemChild('manage_posts', 'create_post');
        $this->addItemChild('manage_posts', 'update_post');
        $this->addItemChild('manage_posts', 'delete_post');
        $this->addItemChild('manage_posts', 'select_post');

        $this->addItemChild('moder', 'user');
        $this->addItemChild('moder', 'manage_posts');
        $this->addItemChild('moder', 'manage_lib');

        $this->addItemChild('super_moder', 'moder');
        $this->addItemChild('super_moder', 'manage_moders');

        $this->addItemChild('admin', 'super_moder');
        $this->addItemChild('admin', 'manage_users');
        $this->addItemChild('admin', 'manage_news');
        $this->addItemChild('admin', 'manage_moders');

        $this->addItemChild('super_admin', 'admin');
        $this->addItemChild('super_admin', 'manage_admins');

        $this->assign('super_admin', 1);

    }

}